//
//  WelcomeViewController.swift
//  XinLangWeiBo
//
//  Created by mac on 15/11/8.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

     // 设置界面，视图的层次结构
    override func loadView() {
         // 直接使用背景图像作为根视图，不用关心图像的缩放问题
        view = backImageView
        
        //设置子控件
        setupUI()
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()

       //异步加载用户头像
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    // 视图已经显示，通常可以动画／键盘处理
    override func viewDidAppear(animated: Bool) {
        // snp_updateConstraints 更新已经设置过的约束
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
            
        }
        
        //动画
    welcomeLabel.alpha = 0
     UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
        // 修改所有`可动画`属性 - 自动布局的动画
        self.view.layoutIfNeeded()
        }) { (_) -> Void in
            UIView.animateWithDuration(0.8, animations: { () -> Void in
                 self.welcomeLabel.alpha = 1
                }, completion: { (_) -> Void in
                    print("OK")
                    
                    
                    //发送通知
            NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
            })
        }
        
    }
    
    
//MARK: - 懒加载控件
    ///背景图像
    lazy var backImageView: UIImageView = UIImageView(imageName: "ad_background")
    
     /// 头像
    lazy var iconView:UIImageView = {

       let iv = UIImageView(imageName: "avatar_default_big")
        // 设置圆角
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        
        return iv
    }()
    
    ///欢迎标签
    lazy var welcomeLabel: UILabel = UILabel(title: "欢迎回来", fontSize: 18)
}

// MARK: - 设置界面
extension WelcomeViewController {
    func setupUI() {
        
        //1 添加控件
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //设置自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.size.equalTo(90)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
}